﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Practices.Unity;
using System.Web.Security;
using COM.Data;
using COM.Service;
using COM.Web;
using SampleApp.Data;
using SampleApp.Service;

namespace SampleApp.Web.Service
{
    public interface IAppLoginService : ILoginService
    {
        bool LoginAsSampleUser(string userCode, string password);
    }

    public class AppLoginService : LoginService, IAppLoginService
    {
        private IAppUserPrincipalFactory appUserPrincipalFactory;
        private ISysUserService sysUserService;
        private IWebPrincipalService webPrincipalService;
        public AppLoginService(ISysUserService sysUserService,IAppUserPrincipalFactory appUserPrincipalFactory, IWebConfigService configService, IWebPrincipalService webPrincipalService)
            : base(appUserPrincipalFactory, configService, webPrincipalService)
        {
            this.sysUserService = sysUserService;
            this.appUserPrincipalFactory = appUserPrincipalFactory;
            this.webPrincipalService = webPrincipalService;
        }

        public bool LoginAsSampleUser(string userCode, string password)
        {
            if (this.sysUserService.CheckUserPassword(userCode, password))
            {
                var user = this.sysUserService.GetSysUserByCode(userCode);
                var p = this.appUserPrincipalFactory.CreateSampleUserPrincipal(userCode, user.UserName, user.DeptCode);
                this.webPrincipalService.SignIn(p);
                return true;
            }
            return false;
        }


    }
}